package window;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.util.Map;

import de.progra.charting.DefaultChart;
import de.progra.charting.model.ObjectChartDataModel;
import de.progra.charting.render.RadarChartRenderer;
import de.progra.charting.swing.ChartPanel;

public class GraphFrame extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ChartPanel panel;
	ObjectChartDataModel data;
	double[][] model = { { 100.0, 100, 100.0 }, {0.0, 50.0, 10.0}, {0.0, 50.0, 0.0} };
	private String[] columns = { "Emission", "Noise and related external costs", "Accidents" };
	private String[]  rows = { "100%", "Goal", "ITS" };
	private final String title = "ITS set performance";
	private final int width = 500, height = 400;
	private Rectangle chartBounds;
	private final double[][] defaultModel = { { 100.0 }, {0.0}, {-1.0} };
	private final String[] defaultColumns = { "default" };

	public GraphFrame() {
		setTitle("Radar Chart");
		setSize(width, height);
		chartBounds = new Rectangle(0, 0, width, height);

//		resetData();
		// Create an object chart data model
		data = new ObjectChartDataModel(model, columns, rows);

		// Creating the Swing ChartPanel instead of DefaultChart
		panel = new ChartPanel(data, title, DefaultChart.NO_COORDINATE_SYSTEM);
		// Adding ChartRenderer as usual
		RadarChartRenderer lcr = new RadarChartRenderer(panel.getCoordSystem(),
				data);
		panel.addChartRenderer(lcr, 1);
		panel.setBounds(chartBounds);
		getContentPane().add(panel, BorderLayout.CENTER);
	}

	public ChartPanel getPanel() {
		panel.setBounds(0, 188, 474, 360); // WHERE TO PUT RADAR!
		return panel;
	}
	
	public void updateChartModel(int i, int j, double value) {
		model[i][j] = value;
		redrawPanel();
	}

	public void updateChartModel(int i, Map<String, Double> results) {
		int idx = -1;
		for(Map.Entry<String, Double> result : results.entrySet()){
				idx = getIdx(result.getKey());
				if(idx >= 0)
					model[i][idx] = result.getValue()*100.0;
				else
					System.err.println("Not found: "+result.getKey());
		}
		redrawPanel();
	}
	
	private void redrawPanel() {
		getContentPane().removeAll();
		data = new ObjectChartDataModel(model, columns, rows);		
		panel = new ChartPanel(data, title, DefaultChart.NO_COORDINATE_SYSTEM);
		// Adding ChartRenderer as usual
		RadarChartRenderer lcr = new RadarChartRenderer(panel.getCoordSystem(), data);
		panel.addChartRenderer(lcr, 1);
		panel.setBounds(chartBounds); // WHERE TO PUT RADAR!
		getContentPane().add(panel);
		panel.revalidate();
		repaint();		
	}

	public static void main(String args[]) {
		// Start the whole shebang	
		new GraphFrame().setVisible(true);
	}
	
	public void resetData(){
		model = defaultModel;
		columns = defaultColumns;
	}

	public int getProbIdx(String problemName) {
		int idx = getIdx(problemName);
		if(idx == -1)
			return addProblem(problemName);
		else
			return idx;
	}

	private int getIdx(String problemName) {
		int idx = -1;
		for(int i=0; i<columns.length; i++){
			if(columns[i].equalsIgnoreCase(problemName)){
				idx = i;
				break;
			}
		}
		return idx;
	}

	private int addProblem(String problemName) {
		columns = resizeColums(problemName);
		model = resizeModel();
		return columns.length-1; // last idx in columns[]
	}

	private double[][] resizeModel() {
		if(model[2][0] < 0.0){
			model[2][0] = 0.0;
			return model;
		}
		
		double newModel[][] = new double[model.length][model[0].length+1];
		for(int i=0; i<model.length; i++){
			int j;
			for(j=0; j<model[0].length; j++){
				newModel[i][j] = model[i][j];
			}
			newModel[i][j] = 0.0;	//default new value
			newModel[0][j] = 100.0; // 100% line
		}
		return newModel;
	}

	private String[] resizeColums(String probName) {		
		if(columns[0].equals("default")){
			columns[0] = probName;
			return columns;
		}
		
		int colSize = columns.length;
		String[] newColumns = new String[colSize+1];
		int i;
		for(i=0; i<colSize; i++)
			newColumns[i] = columns[i];
		newColumns[i] = probName;
		return newColumns;
	}

}
